package com.bdqn.day0916.myexception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:Corday
 * @Date:2021/09/16/8:52
 * 自定义全局异常管理类
 * 自动监控controller中的异常
 */
@ControllerAdvice
public class GlobException {
    //以下为自定义的异常,此处为某一个异常的管理
    //参数，指定管理的异常类型，此处为通用设置
    //更具体的管理需要再定义函数
    @ExceptionHandler(Exception.class)
    public String commException(HttpServletRequest req,Exception e){
        String msg=e.getMessage();
        System.out.println("异常："+msg);
        req.setAttribute("msg",msg);
        //跳转到thymeleaf动态网页
        return "page/error";
    }
    //自定义异常二
    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public void moreException(ArrayIndexOutOfBoundsException e){
        System.out.println("数组越界："+e.getMessage());
    }
}
